﻿using System;
using ProjScan.Config;
using ProjScan.Config.GUIConfig;
using ProjScan.ScanGUI.Page;
using ProjScan.ScanGUI.Tools;
using ProjScan.Tools;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace ProjScan.ScanGUI
{
    //
    // 摘要:
    //     设置主窗口 -
    public class ScanSettingWindow : EditorWindow
    {
        public static ScanSettingWindow I;

        private bool isInit;

        public int fontSize = 15;

        public float pageWidth;

        private List<IPage> pages;

        private GeneralPage generalPage;

        private BasicAssetsCheckPage basicAssetsCheckPage;

        private ScenesCheckPage scenesCheckPage;

        private GlobalSettingsCheckPage globalSettingsCheckPage;

        private CSCheckPage csCheckPage;

        private EffectsPlayCheckPage effectsPlayCheckPage;

        private ShaderAnalysisPage shaderAnalysisPage;

        private ArtAssetsCheckPage artAssetsCheckPage;

        private ScanOverviewPage _scanOverviewPage;

        private string[] moduleNames;

        private int tempHelpBoxFontSize;

        private int tempFoldoutHeaderFontSize;

        private int tempLabelFontSize;

        private int tempPopupFontSize;

        private int selectGrid;

        private Vector2 pageVector2;

        public static void OpenScanSettingWindow()
        {
            ShowWindow();
        }

        private void Init()
        {
            if (!isInit)
            {
                if (I == null)
                {
                    I = EditorWindow.GetWindow<ScanSettingWindow>();
                }

                ReadConfigs();
                Singleton<GUIStyleManager>.Instance.Init(fontSize);
                pages = new List<IPage>();
                generalPage = new GeneralPage();
                scenesCheckPage = new ScenesCheckPage();
                basicAssetsCheckPage = new BasicAssetsCheckPage();
                globalSettingsCheckPage = new GlobalSettingsCheckPage();
                csCheckPage = new CSCheckPage();
                effectsPlayCheckPage = new EffectsPlayCheckPage();
                shaderAnalysisPage = new ShaderAnalysisPage();
                artAssetsCheckPage = new ArtAssetsCheckPage();
                _scanOverviewPage = new ScanOverviewPage();
                pages.Add(generalPage);
                pages.Add(basicAssetsCheckPage);
                pages.Add(scenesCheckPage);
                pages.Add(globalSettingsCheckPage);
                pages.Add(csCheckPage);
                pages.Add(effectsPlayCheckPage);
                pages.Add(shaderAnalysisPage);
                pages.Add(artAssetsCheckPage);
                pages.Add(_scanOverviewPage);
                moduleNames = new string[pages.Count];
                for (int i = 0; i < pages.Count; i++)
                {
                    pages[i].Init();
                    moduleNames[i] = pages[i].pageName;
                }

                isInit = true;
                ToolsHelper.ClearProgressBar();
            }
        }

        private void OnGUI()
        {
            Init();
            SetUnityDefaultFontSize();
            GUI.skin.label.fontSize = fontSize;
            Rect rect = new Rect(5f, 10f, 120f, base.position.height - 10f - 5f);
            GUILayout.BeginArea(rect);
            selectGrid = GUILayout.SelectionGrid(selectGrid, moduleNames, 1, Singleton<GUIStyleManager>.Instance.GetTStyle(TStyle.Toolbar));
            GUILayout.EndArea();
            GUITools.DrawBoxShadow(rect, 2f);
            pageWidth = base.position.width - 120f - 5f - 5f - 3f;
            Rect rect2 = new Rect(5f + rect.width + 3f, 10f, pageWidth, base.position.height - 10f - 5f);
            GUILayout.BeginArea(rect2);
            Rect mainRectangle = new Rect(0f, 0f, rect2.width - 18f, rect2.height);
            GUILayout.BeginHorizontal();
            pageVector2 = EditorGUILayout.BeginScrollView(pageVector2);
            EditorGUILayout.Space();
            DrawRuleState.Initialize(0);
            pages[selectGrid].OnGUI(mainRectangle);
            if (DrawRuleState.EndRule(0))
            {
                Singleton<ConfigMgr>.Instance.SaveAllConfig();
            }

            GUILayout.FlexibleSpace();
            EditorGUILayout.EndScrollView();
            GUILayout.EndHorizontal();
            GUILayout.EndArea();
            GUITools.DrawBoxShadow(rect2, 2f);
            RevertUnityDefaultFontSize();
        }

        private void SetUnityDefaultFontSize()
        {
            tempHelpBoxFontSize = EditorStyles.helpBox.fontSize;
            EditorStyles.helpBox.fontSize = fontSize - 2;
            tempFoldoutHeaderFontSize = EditorStyles.foldoutHeader.fontSize;
            EditorStyles.foldoutHeader.fontSize = fontSize;
            tempLabelFontSize = EditorStyles.label.fontSize;
            tempPopupFontSize = EditorStyles.popup.fontSize;
            EditorStyles.label.fontSize = fontSize;
            EditorStyles.popup.fontSize = fontSize;
        }

        private void RevertUnityDefaultFontSize()
        {
            EditorStyles.helpBox.fontSize = tempHelpBoxFontSize;
            EditorStyles.foldoutHeader.fontSize = tempFoldoutHeaderFontSize;
            EditorStyles.label.fontSize = tempLabelFontSize;
            EditorStyles.popup.fontSize = tempPopupFontSize;
        }

        //
        // 摘要:
        //     读取相关配置
        private static void ReadConfigs()
        {
            Singleton<RuleInfoManager>.Instance.Init();
            Singleton<RuleInfoManager>.Instance.LoadRuleInfo();
            FailedResultManager.Instance.Init();
            FailedResultManager.Instance.LoadAll();
            Singleton<ConfigMgr>.Instance.InitConfig();
            Singleton<ConfigMgr>.Instance.LoadConfig();
        }

        internal static void ShowWindow()
        {
            if (I == null)
            {
                I = EditorWindow.GetWindow<ScanSettingWindow>();
                I.titleContent.text = "Proj Scan";
                I.minSize = new Vector2(1150f, 700f);
                I.Init();
            }

            I.Show();
        }
    }
}